package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.uwlaser.domain.DeviceMain;
import com.uwlaser.service.mapper.DeviceMainMapper;
import com.uwlaser.utils.StringUtils;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.DeviceAlarm;
import com.uwlaser.service.DeviceAlarmService;
import com.uwlaser.service.dto.DeviceAlarmDto;
import com.uwlaser.service.dto.DeviceAlarmQueryParam;
import com.uwlaser.service.mapper.DeviceAlarmMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author zk-Qin
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = DeviceAlarmService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeviceAlarmServiceImpl extends CommonServiceImpl<DeviceAlarmMapper, DeviceAlarm> implements DeviceAlarmService {

    // private final RedisUtils redisUtils;
    private final DeviceAlarmMapper deviceAlarmMapper;

    private final DeviceMainMapper deviceMainMapper;

    @Override
    public PageInfo<DeviceAlarmDto> queryAll(DeviceAlarmQueryParam query, Pageable pageable) {
        if(!StringUtils.isEmpty(query.getPlace())){
            List<DeviceMain> deviceMains = deviceMainMapper.selectList(new LambdaQueryWrapper<DeviceMain>()
                    .likeRight(DeviceMain::getPlace, query.getPlace()));
            if(deviceMains != null && deviceMains.size() > 0){
                DeviceMain deviceMain = deviceMains.get(0);
                String[] split = query.getPlace().split("/");
                if(split.length <= 3) {
                    query.setLineCode(deviceMain.getLineCode());
                }
                if(split.length >= 4) {
                    query.setSectionCode(deviceMain.getSectionCode());
                }
                if(split.length >= 5) {
                    query.setStationCode(deviceMain.getStationCode());
                }
            }
        }
        IPage<DeviceAlarm> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<DeviceAlarm> page = deviceAlarmMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, DeviceAlarmDto.class);
    }

    @Override
    public List<DeviceAlarmDto> queryAll(DeviceAlarmQueryParam query){
        return ConvertUtil.convertList(deviceAlarmMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), DeviceAlarmDto.class);
    }

    @Override
    public DeviceAlarm getById(Integer id) {
        return deviceAlarmMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public DeviceAlarmDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), DeviceAlarmDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(DeviceAlarmDto resources) {
        DeviceAlarm entity = ConvertUtil.convert(resources, DeviceAlarm.class);
        return deviceAlarmMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(DeviceAlarmDto resources){
        DeviceAlarm entity = ConvertUtil.convert(resources, DeviceAlarm.class);
        int ret = deviceAlarmMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return deviceAlarmMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<DeviceAlarmDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (DeviceAlarmDto deviceAlarm : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("设备编码", deviceAlarm.getDeviceCode());
              map.put("设备名称", deviceAlarm.getDeviceName());
              map.put("产线编码", deviceAlarm.getLineCode());
              map.put("工段编码", deviceAlarm.getSectionCode());
              map.put("工位编码", deviceAlarm.getStationCode());
              map.put("报警状态", deviceAlarm.getAlarmStatus());
              map.put("故障信息", deviceAlarm.getAlarmMsg());
              map.put("报警代码", deviceAlarm.getAlarmCode());
              map.put("开始报警时间", deviceAlarm.getStartTime());
              map.put("结束报警时间", deviceAlarm.getEndTime());
              map.put("耗时（分）", deviceAlarm.getTripTime());
              map.put("创建人", deviceAlarm.getCreateBy());
              map.put("创建时间", deviceAlarm.getCreateTime());
              map.put("更新人", deviceAlarm.getUpdateBy());
              map.put("更新时间", deviceAlarm.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
